package com.sniff.atp.service.order;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.sniff.atp.dto.ClientLoginDTO;
import com.sniff.atp.enums.api.OrderAPI;
import com.sniff.atp.enums.status.CustomerOperateType;
import com.sniff.atp.utils.Headers;
import com.sniff.atp.utils.HttpClientUtil;
import com.sniff.atp.utils.JsonUtil;
import com.sniff.atp.utils.LogUtil;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @BelongsProject: sniff-atp
 * @BelongsPackage: com.sniff.atp.service.order
 * @Author: zxy
 * @CreateTime: 2022-11-22 14:36
 * @Description: 异常订单处理
 * @Version: 1.0
 */
@Service
public class AbnormalOrderService {

    /**
     *异常订单，问题处理，未处理查询
     */
    public String queryAbnormal(ClientLoginDTO clientLoginDTO, String platformOrderNo){
        JSONObject param = JsonUtil.readJson("order/abnormal/QueryAbnormal");
        param.replace("platformOrderNo", platformOrderNo);

        return new HttpClientUtil(Headers.setClientHeader(clientLoginDTO)).doPostJson(OrderAPI.ABNORMAL_QUERY.getApi(), param);
    }

    /**
     * 客户问题处理商品
     */
    public String backend(ClientLoginDTO clientLoginDTO, String productCode){
        HashMap<String, String> param = new HashMap<>();
        param.put("productCode", productCode);
        return new HttpClientUtil(Headers.setClientHeader(clientLoginDTO)).doGet(OrderAPI.BACK_END.getApi(), param);
    }

    /**
     * 异常订单：问题处理提交
     */
    public String handleAbnormal(ClientLoginDTO clientLoginDTO, String purchaseAbnormalOrderId, String customerProductLink, String customerProductSku, Integer customerOperateType){
        JSONObject param = JsonUtil.readJson("order/abnormal/HandleAbnormal");
        param.replace("purchaseAbnormalOrderId", purchaseAbnormalOrderId);
        param.replace("customerOperateType", customerOperateType);
        param.replace("customerProductLink", customerProductLink);
        param.replace("customerProductSku", customerProductSku);

        return new HttpClientUtil(Headers.setClientHeader(clientLoginDTO)).doPostJson(OrderAPI.ABNORMAL_HANDLE.getApi(), param);
    }

    /**
     * 异常订单处理流程：
     */
    public void orderAbnormalProcess(ClientLoginDTO clientLoginDTO, String platformOrderNo, CustomerOperateType customerOperateType){
        String results = queryAbnormal(clientLoginDTO, platformOrderNo);
        LogUtil.info("【客户问题联络】查询异常订单：" + platformOrderNo);

        JSONArray records = JsonUtil.extractJSONArray(results, "data.records");

        for (Object i: records) {
            String purchaseAbnormalOrderId = ((JSONObject) i).getString("purchaseAbnormalOrderId");
            String productLink = ((JSONObject) i).getString("productLink");
            String productCode = ((JSONObject) i).getString("productCode");

            String backend = backend(clientLoginDTO, productCode);
            LogUtil.info("【客户问题联络】异常处理订单商品：" + productCode);

            String productSku = JsonUtil.extractString(backend, "data.skuList[0].productSku");
            handleAbnormal(clientLoginDTO, purchaseAbnormalOrderId, productLink, productSku, customerOperateType.getType());
            LogUtil.info("【客户问题联络】异常处理提交，处理类型：" + customerOperateType.getDescription());
        }
    }
}